import { tboxList, tboxAdd, tboxEdit, tboxDelete, fetchTboxById, bindingVin, bindingSim } from '@/services/tboxApi';
import { fetchTboxByDeviceid, fetchVehicleByVin, fetchSimByMsisdn } from '@/services/basedata';

export default {
  namespace: 'tbox',

  state: {
    data: {
      list: [],
      pagination: {},
    },
    tboxData: {
      list: [],
      pagination: {},
    },
    simdata: {},
    vindata: {},
    loading: true,
  },
  effects: {
    *fetchTboxById({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(fetchTboxById, payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *fetchtboxdetail({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(fetchTboxByDeviceid, payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *fetchsimdetail({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(fetchSimByMsisdn, payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *fetchvindetail({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(fetchVehicleByVin, payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *fetch({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(tboxList, payload);
      yield put({
        type: 'tboxSave',
        payload: response,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *add({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(tboxAdd, payload);
      yield put({
        type: 'save',
        payload: response,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *edit({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(tboxEdit, payload);
      yield put({
        type: 'save',
        payload: response,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *bindingSim({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(bindingSim, payload);
      yield put({
        type: 'simsave',
        payload: response,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *bindingVin({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(bindingVin, payload);
      yield put({
        type: 'vinsave',
        payload: response,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *delete({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(tboxDelete, payload);
      yield put({
        type: 'save',
        payload: response,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });

      if (callback) callback(response);
    },
    *clearData({ }, { put }) {
      yield put({
        type: 'changeLoading',
        payload: true
      });
      yield put({
        type: 'tboxSave',
        payload: {
          list: [],
          pagination: {
            current: 0,
            total: 0
          },
          rows: [],
        }
      });
      yield put({
        type: 'changeLoading',
        payload: false
      });
    },
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        data: action.payload,
      };
    },
    tboxSave(state, action) {
      return {
        ...state,
        tboxData: action.payload,
      };
    },
    simsave(state, action) {
      return {
        ...state,
        simdata: action.payload,
      };
    },
    vinsave(state, action) {
      return {
        ...state,
        vindata: action.payload,
      };
    },
    changeLoading(state, action) {
      return {
        ...state,
        loading: action.payload,
      };
    },
  },
};
